<?php

/**
 * Exchange
 *  
 * @author liumingl
 * @version 
 */

require_once 'Zend/Db/Table/Abstract.php';

class Eleven_Resource_Exchange extends CMS_Model_Resource_Db_Table_Abstract {
	protected $_name = 'exchange';
	protected $_primary = 'id';
	protected $_rowClass = 'Eleven_Resource_Exchange_Item';
	
	public function getExchangeContentById($id) {
		return $this->find ( $id )->current ();
	}
	
	public function getExchangeContents($paged = false, $order = null) {
		$select = $this->select ();
		$select->from ( 'exchange' );
		
		if (true === is_array ( $order )) {
			$select->order ( $order );
		}
		
		if (null !== $paged) {
			$adapter = new Zend_Paginator_Adapter_DbTableSelect ( $select );
			$count = clone $select;
			$count->reset ( Zend_Db_Select::COLUMNS );
			$count->reset ( Zend_Db_Select::FROM );
			$count->from ( 'exchange', new Zend_Db_Expr ( 'COUNT(*) AS `zend_paginator_row_count`' ) );
			$adapter->setRowCount ( $count );
			
			$paginator = new Zend_Paginator ( $adapter );
			$paginator->setItemCountPerPage ( 5 )->setCurrentPageNumber ( ( int ) $paged );
			return $paginator;
		}
		return $this->fetchAll ( $select );
	}
}

